<?php
namespace app\overlord\controller;
use app\AdminRest;

use app\ApiRest;
use app\member\model\Member;
use app\overlord\model\Active;
use app\overlord\model\Auth;
use app\overlord\model\Cate;
use app\overlord\model\Content;
use app\overlord\model\Order;
use app\restaurant\model\Store;
use app\restaurant\model\StoreStaff;
use app\shop\controller\IndexWxPay;
use longbingcore\wxcore\WxPay;
use think\App;

use app\overlord\model\Active as model;

use app\overlord\model\Record;

use app\overlord\model\RefundOrder;


use think\facade\Db;


class IndexStaff extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $store;

    protected $order_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->record_model = new Record();

        $this->order_model = new Order();

        $this->refund_order_model = new RefundOrder();

        $staff_model = new StoreStaff();

        $store = $staff_model->getStaffStore($this->getUserId(),1);

        if(empty($store)){

            $this->errorMsg('你不是门店管理员');

        }

        $this->store = $store;



    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:36
     * @功能说明:前端发布活动
     */
    public function atvAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;
        //谁发布对
        $input['user_id'] = $this->getUserId();
        //所属门店
        $input['store']   = [$this->store['store_id']];

        $input['status']  = -1;

        $info = $this->model->check($input);

        if(!empty($info['code'])){

            $this->errorMsg($info['msg']);
        }

        $atv_id    = $this->model->dataAdd($input);

        $order_insert = [

            'uniacid' => $this->_uniacid,

            'order_code' => orderCode(),

            'user_id'   => $this->getUserId(),

            'atv_id'    => $atv_id,

            'pay_price' => round($input['cash']*$input['num'],2),

            'store_id'  => $this->store['store_id']
        ];

        $order_model = new Order();

        $order_model->dataAdd($order_insert);
        //微信支付
        $pay_controller = new IndexWxPay($this->app);
        //支付
        $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"霸王餐",['type' => 'overlord' , 'out_trade_no' => $order_insert['order_code']],$order_insert['pay_price']);

        $arr['pay_list']= $jsApiParameters;

        $arr['atv_id']  = $atv_id;

        return $this->success($arr);


    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function atvUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

            'uniacid' => $this->_uniacid
        ];

        //所属门店
        $input['store']   = [$this->store['store_id']];

        $info = $this->model->check($input);

        if(!empty($info['code'])){

            $this->errorMsg($info['msg']);
        }

        $data = $this->model->adminDataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:50
     * @功能说明:活动详情
     */
    public function atvRecordInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['atv_id']
        ];
        //活动详情
        $data = $this->model->dataInfo($dis);

        $where = [

            'atv_id' => $input['atv_id'],

            'store_id' => $this->store['store_id']
        ];

        $data['record_list'] = $this->record_model->userRecordList($where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 14:27
     * @功能说明:同意
     */
    public function passRecord(){

        $input = $this->_input;

        $update = [

            'sh_time' => time(),

            'text'    => $input['text'],

            'status'  => 3,

            'is_cash' => 1,

            'sh_user' => $this->getUserId()

        ];

        $record = $this->record_model->recordInfo(['id'=>$input['id']]);

        Db::startTrans();

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }
        //增加储值记录,修改储值
        $res = $this->record_model->storedUpdate($record,$this->getUserId(),$this->_uniacid);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }

        Db::commit();

        return $this->success(1);

    }





    /**
     * @author chenniang
     * @DataTime: 2020-11-27 16:48
     * @功能说明:不同意
     */
    public function noPassRecord(){

        $input = $this->_input;

        $update = [

            'sh_time' => time(),

            'text'    => $input['text'],

            'status'  => -1,

            'is_cash' => 0,

            'sh_user' => $this->getUserId()

        ];

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],$update);

        if($res!=1){

            $this->errorMsg('审核失败');
        }

        return $this->success(1);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 16:30
     * @功能说明:活动统计
     */

    public function atvForm(){
         //充值金额
         $data['total_fee']  = $this->order_model->getStoreFee($this->store['store_id']);
         //退款金额
         $data['refund_fee' ]= $this->refund_order_model->getStoreFee($this->store['store_id']);
         //已经领取到金额
         $data['use_fee']    = $this->record_model->storeRecordFee($this->store['store_id']);

         $use_fee            = $this->record_model->storeUseFee($this->store['store_id']);
         //剩余金额
         $data['excess_fee']  = round($data['total_fee'] - $data['refund_fee'] - $use_fee,2);

         $dis[] = ['a.uniacid','=',$this->_uniacid];

         $dis[] = ['a.status','>',-1];

         $dis[] = ['b.store_id','=',$this->store['store_id']];

         $list  = $this->model->indexDataList($dis,10);

         if(!empty($list['data'])){

             foreach ($list['data'] as &$v){
                 //已经领取的金额
                 $v['use_cash'] = $this->record_model->storeRecordFee($this->store['store_id'],$v['id']);

                 $recording     = $this->record_model->recordIng($this->store['store_id'],$v['id']);

                 if($v['atv_end_time']<time()&&$v['use_cash']<$v['total_cash']&&!empty($v['user_id'])&&$v['have_refund']==0&&$recording==0){

                     $v['can_refund'] = 1;

                 }else{

                     $v['can_refund'] = 0;

                 }

             }
         }

         $data['list']  = $list;

         $store_model   = new Store();

         $data['cover'] = $store_model->where(['id'=>$this->store['store_id']])->value('cover');

         return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 09:34
     * @功能说明:分类
     */
    public function cateList(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $cate_model = new Cate();

        $data = $cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 09:37
     * @功能说明:活动列表
     */
    public function activeList(){

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        $dis[] = ['b.store_id','=',$this->store['store_id']];

        if(!empty($input['cate_id'])){

            $dis[] = ['a.cate_id','=',$input['cate_id']];

        }

        $list  = $this->model->indexDataList($dis,10);

        return $this->success($list);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 10:14
     * @功能说明:支付退款记录
     */
    public function orderRecord(){

        $dis = [

            'pay_type' => 1,

            'store_id' => $this->store['store_id']
        ];

        $where = [

            'store_id' => $this->store['store_id']
        ];

        $data = $this->order_model->orderFrom($dis,$where,10);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:03
     * @功能说明:报名详情
     */
    public function activeInfo(){

        $input = $this->_input;

        $atctive_model = new Active();

        $data  = $atctive_model->dataInfo(['id'=>$input['atv_id']]);

        $data['list'] = $this->record_model->userRecordList(['a.atv_id'=>$input['atv_id'],'a.store_id'=>$this->store['store_id']]);

        if(!empty($data['list']['data'])){

            foreach ($data['list']['data'] as $k=>$v){

                $data['list']['data'][$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:19
     * @功能说明:下拉框
     */
    public function contentSelect(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $content_model = new Content();

        $data = $content_model->where($dis)->field('id,title')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 17:30
     * @功能说明:申请退款
     */
    public function applyOrder(){

        $input = $this->_input;

        $atv   = $this->model->dataInfo(['id'=>$input['atv_id']]);

        if(empty($atv['user_id'])){

            $this->errorMsg('后台创建活动，不能退款');
        }

        $res = $this->model->where(['id'=>$input['atv_id']])->update(['have_refund'=>1]);

        if($res!=1){

            $this->errorMsg('申请失败');
        }

        $use_fee = $this->record_model->storeUseFee($this->store['store_id'],$input['atv_id']);

        $refund_fee = round($atv['total_cash'] - $use_fee,2);

        if($refund_fee<=0){

            $this->errorMsg('余额不足');
        }

        $order_id   = $this->order_model->where(['atv_id'=>$input['atv_id'],'pay_type'=>1])->value('id');

        $insert = [

            'uniacid'      => $this->_uniacid,

            'user_id'      => $atv['user_id'],

            'order_id'     => $order_id,

            'refund_price' => $refund_fee,

            'status'       => 1,

            'store_id'     => $this->store['store_id'],

            'atv_id'       => $input['atv_id'],

            'order_code'   => orderCode()

        ];

        $res = $this->refund_order_model->dataAdd($insert);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-01 15:55
     * @功能说明:是否有发布活动的权限
     */
    public function activeAuth(){

        $auth_model = new Auth();

        $data = $auth_model->where(['store_id'=>$this->store['store_id'],'status'=>1])->find();

        $res = !empty($data)?1:0;

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-03 09:56
     * @功能说明:
     */
    public function getStoreId(){


        return $this->success($this->store['store_id']);

    }

}
